<template>
    <el-dialog title="加入平台" :visible.sync="is_add_group_form_show" :before-close="handleClose">
        <el-form class="dialog-form" label-width="120px">
            <el-form-item label="群编号：">
                <el-input v-model="id"></el-input>
            </el-form-item>
            <el-form-item label="我方开放部门：">
                <el-tree ref="add-group-tree" :data="sys_department_tree" check-strictly show-checkbox node-key="id" :props="props"></el-tree>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleClose">取消</el-button>
                <el-button @click="onSubmit" type="primary">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import group from '@/api/group.js';
import {mapGetters} from 'vuex';

export default {
    name: 'add-group-dialog',
    props: ['is_add_group_form_show', 'data'],
    computed: {
        ...mapGetters('department1', ['sys_department_tree'])
    },
    data() {
        return {
            id: undefined,
            props: {
                children: 'children',
                value: 'id',
                label: 'text'
            }
        }
    },
    methods: {
        handleClose() {
            this.id = undefined;
            this.$refs['add-group-tree'].setCheckedKeys([]);
            this.$emit('update:is_add_group_form_show', false);
        },
        onSubmit() {
            let arr = this.$refs['add-group-tree'].getCheckedKeys();
            if (arr.length === 0) {
                this.$message.error('请选择部门!');
                return false;
            }
            if (this.id === undefined) {
                this.$message.error('请输入平台编号!');
                return false;
            }
            group.applyJoinGroup({groupId: this.id,department: arr}).then(res => {
                this.$message({
                    type: 'success',
                    message: '添加平台成功!'
                });
                this.$store.dispatch('group/getJoinGroupList');
            }).catch(err => {
                this.$message.error(err);
            });
            this.handleClose();
            arr = null;
        }
    }
}
</script>

<style>

</style>
